﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.SdkTypeProxy.Metadata;

namespace Custom.Crm.Sdk
{
    public class CustomDynamicEntity : Microsoft.Crm.Sdk.DynamicEntity
    {
        public string GetAttributeValue(string attributeName)
        {
            return CustomDynamicEntity.GetAttributeValue(this.Properties[attributeName], false);
        }

        public static string GetAttributeValue(object prop, bool textValue)
        {
            if (prop == null)
                return null;

            Type propertyType = prop.GetType();
            string propertyValue = null;

            if (propertyType == typeof(string))
            {
                propertyValue = prop.ToString();
            }
            else if (propertyType == typeof(CrmDateTime))
            {
                propertyValue = ((CrmDateTime)prop).Value.ToString();
            }
            else if (propertyType == typeof(Key))
            {
                propertyValue = ((Key)prop).Value.ToString("D");
            }
            else if (propertyType == typeof(Customer))
            {
                if (textValue)
                {
                    propertyValue = ((Customer)prop).name;
                }
                else
                {
                    propertyValue = ((Customer)prop).Value.ToString();
                }
            }
            else if (propertyType == typeof(Lookup))
            {
                if (textValue)
                {
                    propertyValue = ((Lookup)prop).name;
                }
                else
                {
                    propertyValue = ((Lookup)prop).Value.ToString("D");
                }
            }
            else if (propertyType == typeof(Owner))
            {
                if (textValue)
                {
                    propertyValue = ((Owner)prop).name;
                }
                else
                {
                    propertyValue = ((Owner)prop).Value.ToString("D");
                }
            }
            else if (propertyType == typeof(CrmBoolean))
            {
                propertyValue = ((CrmBoolean)prop).Value.ToString();
            }
            else if (propertyType == typeof(Picklist))
            {
                if (textValue)
                {
                    propertyValue = ((Picklist)prop).name.ToString();
                }
                else
                {
                    propertyValue = ((Picklist)prop).Value.ToString();
                }
            }
            else if (propertyType == typeof(CrmDecimal))
            {
                propertyValue = ((CrmDecimal)prop).Value.ToString();
            }
            else if (propertyType == typeof(CrmFloat))
            {
                propertyValue = ((CrmFloat)prop).Value.ToString();
            }
            else if (propertyType == typeof(CrmMoney))
            {
                propertyValue = ((CrmMoney)prop).Value.ToString();
            }
            else if (propertyType == typeof(CrmNumber))
            {
                propertyValue = ((CrmNumber)prop).Value.ToString();
            }
            else if (propertyType == typeof(Status))
            {
                if (textValue)
                {
                    propertyValue = ((Status)prop).name.ToString();
                }
                else
                {
                    propertyValue = ((Status)prop).Value.ToString();
                }
            }

            return propertyValue;

        }

        public static string GetPropertyValue(Property property)
        {
            return CustomDynamicEntity.GetPropertyValue(property, false);
        }

        public string GetPropertyValue(string propertyName)
        {
            return CustomDynamicEntity.GetPropertyValue((Property)this.Properties[propertyName], false);
        }

        public static string GetPropertyValue(Property prop, bool textValue)
        {
            Type propertyType = prop.GetType();
            string propertyValue = null;

            if (propertyType == typeof(StringProperty))
            {
                propertyValue = ((StringProperty)prop).Value.ToString();
            }
            else if (propertyType == typeof(CrmDateTimeProperty))
            {
                propertyValue = ((CrmDateTimeProperty)prop).Value.Value.ToString();
            }
            else if (propertyType == typeof(KeyProperty))
            {
                propertyValue = ((KeyProperty)prop).Value.Value.ToString("D");
            }
            else if (propertyType == typeof(CustomerProperty))
            {
                if (textValue)
                {
                    propertyValue = ((CustomerProperty)prop).Value.name;
                }
                else
                {
                    propertyValue = ((CustomerProperty)prop).Value.Value.ToString();
                }
            }
            else if (propertyType == typeof(LookupProperty))
            {
                if (textValue)
                {
                    propertyValue = ((LookupProperty)prop).Value.name;
                }
                else
                {
                    propertyValue = ((LookupProperty)prop).Value.Value.ToString("D");
                }
            }
            else if (propertyType == typeof(StateProperty))
            {
                propertyValue = ((StateProperty)prop).Value.ToString();
            }
            else if (propertyType == typeof(OwnerProperty))
            {
                if (textValue)
                {
                    propertyValue = ((OwnerProperty)prop).Value.name;
                }
                else
                {
                    propertyValue = ((OwnerProperty)prop).Value.Value.ToString("D");
                }
            }
            else if (propertyType == typeof(CrmBooleanProperty))
            {
                propertyValue = ((CrmBooleanProperty)prop).Value.Value.ToString();
            }
            else if (propertyType == typeof(PicklistProperty))
            {
                if (textValue)
                {
                    propertyValue = ((PicklistProperty)prop).Value.name.ToString();
                }
                else
                {
                    propertyValue = ((PicklistProperty)prop).Value.Value.ToString();
                }
            }
            else if (propertyType == typeof(CrmDecimalProperty))
            {
                propertyValue = ((CrmDecimalProperty)prop).Value.Value.ToString();
            }
            else if (propertyType == typeof(CrmFloatProperty))
            {
                propertyValue = ((CrmFloatProperty)prop).Value.Value.ToString();
            }
            else if (propertyType == typeof(CrmMoneyProperty))
            {
                propertyValue = ((CrmMoneyProperty)prop).Value.Value.ToString();
            }
            else if (propertyType == typeof(CrmNumberProperty))
            {
                propertyValue = ((CrmNumberProperty)prop).Value.Value.ToString();
            }

            return propertyValue;
        }
    }
}
